package com.msc.controller.subscribe;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.msc.common.lang.Result;
import com.msc.common.lang.exception.CiliciliErrorCodeEnum;
import com.msc.common.lang.exception.CiliciliException;
import com.msc.common.lang.exception.ICiliciliErrorCode;
import com.msc.entity.Blog;
import com.msc.entity.SubRecord;
import com.msc.entity.Subscribe;
import com.msc.service.SubRecordService;
import com.msc.service.SubscribeService;
import com.msc.util.ShiroUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author masc
 * @date 2022/6/25 20:51
 * <p>订阅记录</p>
 */
@RestController
@RequestMapping("/sub")
public class SubscribeRecordeController {

    @Resource
    private SubRecordService subRecordService;
    @Resource
    private SubscribeService subscribeService;

    @RequiresPermissions("user:source")
    @RequestMapping("/getAllSubRecordByUserId")
    public Result getAllScribeInfoListByUserId(@RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex,
                                               @RequestParam(value = "userId", defaultValue = "") Integer userId,
                                               @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        if (null == userId) {
            throw new CiliciliException(CiliciliErrorCodeEnum.No_AUTHORITY);
        }
        IPage<SubRecord> pageList = subRecordService.getAllScribeRecordByUserId(userId, pageIndex, pageSize);
        return Result.succ(pageList);
    }

    //管理后台查询所有订阅记录
    @RequiresPermissions("user:source")
    @RequestMapping("/getAllSubRecord")
    public Result getAllScribeInfoList(@RequestParam(value = "pageIndex", defaultValue = "1") int pageIndex,
                                       @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {

        QueryWrapper<SubRecord> queryWrapper = new QueryWrapper<>();
        Page<SubRecord> page = new Page<>(pageIndex, pageSize);
        IPage<SubRecord> pageList = subRecordService.page(page, queryWrapper);
        return Result.succ(pageList);
    }

    /**
     * 创建订阅记录
     */
    //    @RequiresPermissions("user:create")
//    @RequiresAuthentication
    @PostMapping("/subRecord/create")
    public Result saveOrUpdate(@RequestBody SubRecord subRecord) {
        SubRecord temp = null;
        if (subRecord.getId() != null) {
            temp = subRecordService.getById(temp.getId());
        } else {
            temp = new SubRecord();
            temp.setUserId(ShiroUtil.getProfile().getId());
            temp.setCreateTime(LocalDateTime.now());
        }
        temp.setUpdateTime(LocalDateTime.now());
        BeanUtil.copyProperties(subRecord, temp, "id", "userId", "createTime", "updateTime");
        boolean b = subRecordService.saveOrUpdate(temp);
        return Result.succ(b);
    }

    /**
     * 查看订阅详情byId
     */
//    @RequiresPermissions("user:read")
    @GetMapping("/subRecord/detailById/{id}")
    public Result getDetail(@PathVariable(name = "id") Long id) {
        Long userId = ShiroUtil.getProfile().getId();
        QueryWrapper<SubRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SubRecord::getUserId, userId).eq(SubRecord::getId, id);
        SubRecord subRecord = subRecordService.getOne(queryWrapper);
        if (null != subRecord) {
            Long subscribeId = subRecord.getSubscribeId();
            Subscribe byId = subscribeService.getById(subscribeId);
            return Result.succ(byId);
        }
        return Result.succ(null);
    }

}
